﻿using CarPCBATest.Procedure;
using Common;
using Device;
using ImageSourceModuleCs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using VM.Core;

namespace CarPCBATest.ViewModel
{
    public partial class HomeViewModel
    {
        /// <summary>
        /// 控制板测试逻辑，控制板须作为最后一个测试项 测完其他的LED板之后，如果其他LED板测试通过则判定控制板通过，否则NG
        /// </summary>
        /// <param name="config"></param>
        /// <param name="index"></param>
        public void TestMainBoard(SingBoardConfig config, int index)
        {
            if (Products.Exists(x => x.IsOk == false))
            {
                Products[index].CurrentIsOK = false;
                Products[index].VisionIsOK = false;
            }
        }

        private ManualResetEventSlim visionCompleteEvent;

        public void TestLedBoard(SingBoardConfig config, int index)
        {
            Log = $"开始测试{config.LedDesc}功能";
            // 设置电压
            IV_3003T.SetChStatus(0);    //关闭电源
            for (int i = 0; i < config.ListPowerConfig.Count; i++)
            {
                IV_3003T.SwitchCh((IV3003T_CH_ENUM)(i + 1));
                IV_3003T.SetVoltage(config.ListPowerConfig[i].Voltage);
                IV_3003T.SetCurrent(config.ListPowerConfig[i].Current);
            }

            // 设置电源IO
            for (int i = 0; i < config.ListDOConfig.Count; i++)
            {
                IOModuleDO.SetDOValue((DODefine)config.ListDOConfig[i].DOFunction, config.ListDOConfig[i].IsOn ? 1 : 0);
            }
            IV_3003T.SetChStatus(1);    //打开电源

            Thread.Sleep(config.WaitTime);

            // 电流测试 读取电流
            double current = DM3058E.GetValue(Function.DCI) * 1000;
            Log = $"工位[{config.StationNo}]功能[{config.LedDesc}]电流为{current}mA";

            Products[index].TestCurrent = current.ToString();
            Products[index].StdCurrent = $"{config.LowerLimitCurrent}-{config.UpperLimitCurrent}mA";
            if (current <= config.UpperLimitCurrent & current >= config.LowerLimitCurrent)
            {
                // ok
                Products[index].CurrentIsOK = true;
            }
            else
            {
                //ng
                Products[index].CurrentIsOK = false;
                Products[index].NGMessages.Add($"电流=[{current}]电流测试NG");
                Log = $"工位[{config.StationNo}]功能[{config.LedDesc}]电流不在OK范围!";
            }

            // 视觉检测

            Application.Current.Dispatcher.Invoke(() =>
            {
                VisionTool.SetProcVisionRet(config.ProcName, control);
            });

            visionCompleteEvent = new ManualResetEventSlim(false);
            currentProcId = config.ProcID;
            // sdk获取图像
            CameraTool.Open();
            var image = CameraTool.GetCurrentImage();
            //设置到图像源
            ImageSourceModuleTool imageSourceModuleTool = (ImageSourceModuleTool)VmSolution.Instance[$"{config.ProcName}.图像源1"];
            imageSourceModuleTool.SetImageData(image);
            VisionTool.SetGlobalVar("snCurrent", Products[index].SN +"_" + config.LedDesc);

            VisionTool.RunProc(config.ProcName);

            if (!visionCompleteEvent.Wait(30000))
            {
                // 视觉流程执行超时
                throw new Exception("视觉判定流程执行超时!");
            }
            if (vmResult is null)
            {
                throw new Exception("视觉判定测试结果为null!");
            }
            Mylog.Info($"vmResult=[{vmResult}]");

            if (vmResult == "0")
            {
                Log = $"工位[{config.StationNo}]功能[{config.LedDesc}]灯光视觉异常!";
                Products[index].NGMessages.Add("视觉检测NG!");
                Products[index].VisionIsOK = false;
            }
            else
            {
                Products[index].VisionIsOK = true;
            }
        }
    }
}